package com.xumz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xumz.controller.tool.IDFactoty;
import com.xumz.dao.RecordDao;
import com.xumz.dao.TeacherDao;
import com.xumz.model.Record;
import com.xumz.model.RecordManage;
import com.xumz.model.StudentManage;
import com.xumz.model.Teacher;
import com.xumz.service.crawler.Boke;
import com.xumz.service.RecordService;
@Service
public class RecordServiceImpl implements RecordService {
	@Autowired
	private RecordDao dao;
	@Autowired
	private TeacherDao teacherDao;
	@Override
	public Record selectOne(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Record> selectAll(String id) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public List<Record> selectAll() {
		List<Record> list=dao.selectAll();
		return list;
	}
	
	@Override
	public List<RecordManage> selectByStudentId(String id) {
		List<RecordManage> list=dao.selectAllRecordManage();
//		System.out.println("list:"+list);
		List<RecordManage> resultList=new ArrayList<RecordManage>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getStudentId().equals(id)) {
				resultList.add(list.get(i));
			}
		}
//		System.out.println("resultList:"+resultList);
		return resultList;
	}
	
	@Override
	public List<Record> selectRecordByStudentId(String id) {
		List<Record> list=dao.selectAll();
		List<Record> resultList=new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getStudentId().equals(id)) {
				resultList.add(list.get(i));
			}
		}
		return resultList;
	}
	
	@Override
	public List<Record> getCrawler(String url,String sid){
		List<Record> list=new ArrayList<Record>();
		Boke boke=new Boke(url+"p/");
		List<String> titleList=boke.bokeTitleList();
		List<String> urlList=boke.bokeUrlList();
		List<String> timeList=boke.bookTimeList();
		List<String> readNumList=boke.bookReadNumList();
		List<String> commentNumList=boke.bookCommentNumList();
		for (int i = 0; i < titleList.size(); i++) {
			Record record=new Record();
			if (sid==null) {
				sid="0";
			}
			record.setStudentId(sid);
			record.setBlogId(IDFactoty.productIdByDateTime());
			record.setBlogTitle(titleList.get(i));
			record.setBlogUrl(urlList.get(i));
			record.setBlogTime(timeList.get(i));
			record.setBlogReadTimes(readNumList.get(i));
			record.setBlogCommentTimes(commentNumList.get(i));
			list.add(record);
			System.out.println(record);
		}
		return list;
	}

	@Override
	public int insertOne(Record record) {
		dao.insertOne(record);
		return 0;
	}

	@Override
	public int insertList(List<Record> list) {
		for (int i = 0; i < list.size(); i++) {
			dao.insertOne(list.get(i));
		}
		return 0;
	}

	@Override
	public int deleteOne(String id) {		
		return dao.deleteOne(id);
	}

	@Override
	public List<RecordManage> selectAllRecordManage() {
		return dao.selectAllRecordManage();
	}
	
	@Override
	public List<RecordManage> selectAllRecordManage(String num) {
		return dao.selectAllRecordManage(num);
	}
	
	@Override
	public List<RecordManage> selectAllRecordManageByTeacherName(String name) {
		Teacher teacher=teacherDao.selectOne(name);
		return dao.selectAllRecordManageByTeacherId(teacher.getId());
	}

	@Override
	public List<RecordManage> selectRecordManageByOwer(List<RecordManage> list,String owner) {
		for (int i = 0; i < list.size(); i++) {
			if (!list.get(i).getStuName().contains(owner)) {
				list.remove(i);
				i--;
			}
		}
		return list;
	}

	@Override
	public List<RecordManage> selectRecordManageByKeyWord(
			List<RecordManage> list, String keyWord) {
		for (int i = 0; i < list.size(); i++) {
			if (!list.get(i).getBlogTitle().contains(keyWord)) {
				list.remove(i);
				i--;
			}
		}
		return list;
	}

}
